<template>
  <div class="login-container">
    <div class="login-bg"></div>
    <el-card class="login-card">
      <div class="logo-container">
        <h2>乡村旅游系统 -注册</h2>
      </div>

      <el-form
        ref="registerFormRef"
        @submit.prevent="handleRegister"
        :model="state.registerForm"
        :rules="state.formRules"
      >
        <el-form-item prop="username">
          <el-input v-model="state.registerForm.username" placeholder="请输入账号" prefix-icon="User" clearable />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="state.registerForm.password"
            type="password"
            show-password
            placeholder="请输入密码"
            prefix-icon="Lock"
            clearable
          />
        </el-form-item>

        <el-form-item prop="realName">
          <el-input v-model="state.registerForm.realName" placeholder="请输入真实姓名" prefix-icon="User" clearable />
        </el-form-item>

        <el-form-item prop="mobile">
          <el-input v-model="state.registerForm.mobile" placeholder="请输入手机号码" prefix-icon="phone" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" native-type="submit" :loading="state.loading" class="login-btn"> 注 册 </el-button>
          <el-button plain @click="router.push('/login')" class="register-btn"> 登录账号 </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useLoginApi } from '@/api/login'

const router = useRouter()
const loginApi = useLoginApi()

const registerFormRef = ref()
const state = reactive({
  registerForm: {
    username: '',
    password: '',
    realName: '',
    mobile: ''
  },
  formRules: {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' },
      { min: 4, max: 16, message: '长度在4到16个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 18, message: '长度在6到18个字符', trigger: 'blur' }
    ],
    realName: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }],
    mobile: [
      { required: true, message: '请输入手机号码', trigger: 'blur' },
      {
        pattern: /^1[3456789]\d{9}$/,
        message: '手机号码格式错误',
        trigger: ['blur', 'change']
      }
    ]
  },
  loading: false
})

// 处理登录
const handleRegister = async () => {
  try {
    await registerFormRef.value.validate()
    state.loading = true
    await loginApi.register(state.registerForm)
    ElMessage.success('注册成功')
    router.push('/login')
  } catch (err) {
    console.log(err)
  } finally {
    state.loading = false
  }
}
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 50%;
    background: linear-gradient(135deg, #409eff, #67c23a);
    z-index: 1;
  }
}

.login-card {
  width: 350px;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
  z-index: 2;
  position: relative;

  .logo-container {
    text-align: center;
    margin-bottom: 20px;

    .logo {
      width: 80px;
      height: 80px;
      margin-bottom: 15px;
    }

    h2 {
      color: #303133;
      font-size: 22px;
      font-weight: 600;
      margin: 0;
    }
  }

  :deep(.el-form-item__label) {
    padding-bottom: 8px;
    font-weight: 500;
  }

  .captcha-container {
    display: flex;
    align-items: center;
    gap: 10px;

    .captcha-image {
      width: 120px;
      height: 30px;
      border-radius: 4px;
      cursor: pointer;
      border: 1px solid #dcdfe6;
    }
  }

  .login-btn {
    width: 100%;
    margin-top: 10px;
  }

  .register-btn {
    width: 100%;
    margin: 0;
    margin-top: 10px;
  }
}
</style>
